package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品及对应口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        // 向菜品表插入1条数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insert(dish);
        Long dishId=dish.getId();
        // 向口味表插入n条数据
        List<DishFlavor> list= dishDTO.getFlavors();
        if(list!=null && !list.isEmpty()){
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(list);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQueryDish(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishByIds(List<Long> ids) {
        // 判断状态是否起售中
        for(Long id:ids){
            Dish dish = dishMapper.selectDishById(id);
            if(dish.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 判断是否关联了套餐
        List<Long> setmealIds = setmealDishMapper.selectSetmealIdByDishId(ids);
        if(setmealIds!=null&&!setmealIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除菜品中的数据及口味表中的对应数据
        dishMapper.deleteBatchDish(ids); //批量删除减少sql访问次数
        dishFlavorMapper.deleteBatchFlavor(ids);
//        for(Long id:ids) {
//            dishMapper.deleteDishById(id);
//            dishFlavorMapper.deleteFlavorByDishId(id);
//        }
    }

    @Override
    @Transactional
    public void updateDishInfo(DishDTO dishDTO) {
        Dish dish=new Dish();
        Long dishId=dishDTO.getId();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteFlavorByDishId(dishId);
        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null&&!list.isEmpty()){
            list.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(list);
        }
    }

    /**
     * 根据id获取菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        Dish dish=dishMapper.selectDishById(id);
        List<DishFlavor> list=dishFlavorMapper.getFlavorById(dish.getId());
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    /**
     * 修改菜品售卖状态
     * 同时修改包含该菜品的套餐售卖状态
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void switchStatus(Integer status, Long id) {
        Dish dish=Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
        if(status==StatusConstant.DISABLE){
            List<Long> dishIds=new ArrayList<>();
            dishIds.add(id);
            List<Long> setmealIds=setmealDishMapper.selectSetmealIdByDishId(dishIds);
            for(Long setmealId:setmealIds) {
                Setmeal setmeal=Setmeal.builder()
                        .id(setmealId)
                        .status(status)
                        .build();
                setmealMapper.update(setmeal);
            }
        }
    }

    /**
     * 获取在售菜品列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listDish(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.listDish(dish);
    }

    /**
     * 根据类目id获取起售菜品及对应口味
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> listDishVOOnSale(Long categoryId) {
        // 查询起售菜品
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list=dishMapper.listDish(dish);
        List<DishVO> result=new ArrayList<>();
        // 获取菜品口味数据并封装
        for(Dish item:list){
            List<DishFlavor> flavors=dishFlavorMapper.getFlavorById(item.getId());
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(item,dishVO);
            dishVO.setFlavors(flavors);
            result.add(dishVO);
        }
        return result;
    }
}
